package com.choupin.choupin.controller;

import cn.hutool.json.JSON;
import com.choupin.choupin.entity.JsonResult;
import com.choupin.choupin.entity.Mine;
import com.choupin.choupin.entity.User;
import com.choupin.choupin.service.impl.UserServiceImpl;
import org.omg.PortableInterceptor.SUCCESSFUL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.reactive.context.StandardReactiveWebEnvironment;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.annotation.PostConstruct;
import javax.jws.soap.SOAPBinding;
import javax.security.sasl.SaslServer;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController extends BaseController {

     @Autowired
    UserServiceImpl userService;


    //常规注册的
    @CrossOrigin
    @PostMapping("/register")
    public JsonResult<Void> register(@RequestBody Map<String,Object> data  ){
        JsonResult<Void> jsonResult=new JsonResult<>();
        System.out.println(data.get("phone")+","+data.get("gender"));
        User user=new User();
        user.setPhone((String) data.get("phone"));
        user.setPassword((String) data.get("password"));
        user.setGender((Integer) data.get("gender"));
        String dataString= (String) data.get("birthday");
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date=simpleDateFormat.parse(dataString);
            user.setBirthday(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        userService.register(user);
        jsonResult.setState(SUCCESS);
        return jsonResult;
    }

    //密码登录
    @CrossOrigin
    @PostMapping("/loginpwd")
    public JsonResult<Void> loginpwd(@RequestBody Map<String,Object> data, HttpSession session){
      JsonResult<Void> jsonResult=new JsonResult<>();
       userService.pwdlogin((String) data.get("phone"),(String) data.get("password"),session);
       jsonResult.setState(SUCCESS);
      return jsonResult;
    }

//    发送验证码
@CrossOrigin
    @PostMapping("/sendmessage")
    public JsonResult<Void> sendmessage(@RequestBody Map<String,Object> data){
    System.out.println("获取验证码来到这里！");
         JsonResult<Void> jsonResult=new JsonResult<>();
         userService.sendmessage((String) data.get("phone"));
         jsonResult.setState(SUCCESS);
         return jsonResult;
    }

//     短信登录
@CrossOrigin
    @PostMapping("/loginmessage")
    public JsonResult<Void> loginmessage(@RequestBody Map<String,Object> data){
         JsonResult<Void> jsonResult=new JsonResult<>();
    System.out.println("获取短信验证");
         userService.messagelogin((String) data.get("code"),(String) data.get("phone"));
         jsonResult.setState(SUCCESS);
         return jsonResult;
     }

//     查看个人信息
    @CrossOrigin
    @PostMapping("/mineshow")
    public JsonResult<Mine> mineShow(@RequestBody Map<String,Object> data){
         JsonResult<Mine> jsonResult=new JsonResult<>();
         Mine mine=userService.mineShow((String) data.get("phone"));
        System.out.println(mine);
         jsonResult.setData(mine);
         jsonResult.setState(SUCCESS);
         return  jsonResult;
    }

//    更新头像
    @CrossOrigin
    @PostMapping("/uploadavatar")
    public JsonResult<Void> uploadAvatar(@RequestParam("file") MultipartFile file,@RequestParam("phone") String phone ) throws Exception {
         JsonResult<Void> jsonResult=new JsonResult<>();
         userService.uploadAvatar(file,phone);
         jsonResult.setState(SUCCESS);
         return jsonResult;
    }

//    下载图片（没用到）
    @CrossOrigin
    @PostMapping("/download")
    public  JsonResult<String> downloadIages(@RequestBody Map<String,Object> data){
        JsonResult<String> jsonResult=new JsonResult<>();
        System.out.println("下载图片获取到的图片路径："+(String) data.get("filePath"));
        String result=userService.downloadImages((String) data.get("filePath"));
        jsonResult.setState(SUCCESS);
        jsonResult.setData(result);
        return  jsonResult;
    }

//    更新个人信息
    @CrossOrigin
    @PostMapping("/updatemine")
    public JsonResult<Void> updateMine(@RequestBody Map<String,Object> data ){
         JsonResult<Void> jsonResult=new JsonResult<>();
         User user=new User();
         user.setPhone((String) data.get("phone"));
        System.out.println("获取到的更新年龄："+(int) data.get("age"));
         user.setAge((int) data.get("age"));
         user.setNickname((String)data.get("nickname"));
         user.setPassword((String) data.get("password"));
         user.setGender((Integer) data.get("gender"));
        System.out.println("更新性别为："+user.getGender());
         String birthdaystr= (String)data.get("birthday");
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date birthday=simpleDateFormat.parse(birthdaystr);
            user.setBirthday(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        userService.updateMine(user,(String) data.get("newPassword"),(String)data.get("confirmPassword"));
        jsonResult.setState(SUCCESS);
         return  jsonResult;
    }

//    注销
    @CrossOrigin
    @PostMapping("/logout")
     public  JsonResult<Void> logOut(@RequestBody Map<String,Object> data ){
         JsonResult<Void> jsonResult=new JsonResult<>();
          userService.logout((String) data.get("phone"));
          jsonResult.setState(SUCCESS);
         return jsonResult;
     }

}

